package com.example.controller;

import com.common.result.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.pojo.dto.QueryPageDTO;
import com.pojo.entity.User;
import com.example.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author ccz
 * @since 2025-07-19
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 新增用户
     */
    @PostMapping
    public Result save(@RequestBody User user) {
        userService.save(user);
        return Result.success("新增用户成功");
    }

    /**
     * 分页查询用户
     */
    @GetMapping("/page")
    public Result page(QueryPageDTO queryPageDTO) {
        PageHelper.startPage(queryPageDTO.getPageNum(), queryPageDTO.getPageSize());
        List<User> list = userService.list();
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    /**
     * 查询所有用户
     */
    @GetMapping
    public Result list() {
        List<User> list = userService.list();
        return Result.success(list);
    }

    /**
     * 根据ID查询用户
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 修改用户信息
     */
    @PutMapping
    public Result update(@RequestBody User user) {
        userService.updateById(user);
        return Result.success("修改用户成功");
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        userService.removeById(id);
        return Result.success("删除用户成功");
    }

    /**
     * 批量删除用户
     */
    @DeleteMapping("/batch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        userService.removeByIds(ids);
        return Result.success("批量删除用户成功");
    }
}
